package utils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public abstract class FRefreshHandler {
    private ArrayList<FBean> data = new ArrayList<>();
    private JTable table;
    private int[] sizes = new int[]{0, 0, 0/*, 0*/};

    public FRefreshHandler(JTable table) {
        this.table = table;
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    }

    /**
     * 从网络更新数据
     *
     * @param code
     */
    public abstract void handle(List<String> code);

    /**
     * 更新全部数据
     */
    public void updateUI() {
        SwingUtilities.invokeLater(() -> {
            recordTableSize();
            String[] columnNames = {"Name", /*"Value",*/ "Trend", "Up Time"};
            DefaultTableModel model = new DefaultTableModel(convertData(), columnNames);
            table.setModel(model);
            updateColors();
            resizeTable();

        });
    }

    private void recordTableSize() {
        setSizeOrNot(table, sizes);
    }

    private static TableColumn getColumn(JTable table, Object obj) {
        TableColumnModel cm = table.getColumnModel();
        int index;
        if (obj instanceof Integer) {
            index = (int) obj;
        } else {
            index = cm.getColumnIndex(obj);
        }
        return cm.getColumn(index);
    }

    static void setSizeOrNot(JTable table, int[] sizes) {
        if (table.getColumnModel().getColumnCount() == 0) {
            return;
        }
        for (int i = 0; i < sizes.length; i++) {
            sizes[i] = getColumn(table, i).getWidth();
        }
    }

    private void resizeTable() {
        for (int i = 0; i < sizes.length; i++) {
            if (sizes[i] > 0) {
                //if (i == 0 || i == 1) sizes[i] = 0;
                getColumn(table, i).setWidth(sizes[i]);
                getColumn(table, i).setPreferredWidth(sizes[i]);
            }
        }
    }

    private void updateColors() {
        getColumn(table,"Trend").setCellRenderer(ColorUtils.getDefaultRenderer());
    }

    protected void updateData(FBean bean) {
        int index = data.indexOf(bean);
        if (index >= 0) {
            data.set(index, bean);
        } else {
            data.add(bean);
        }
    }

    private Object[][] convertData() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Object[][] temp = new Object[data.size()][4];
        for (int i = 0; i < data.size(); i++) {
            FBean fBean = data.get(i);
            String timeStr = fBean.getGztime();

            String today = dateFormat.format(new Date());
            if (timeStr != null && timeStr.startsWith(today)) {
                timeStr = timeStr.substring(timeStr.indexOf(" "));
            }
            String gszzlStr = "--";
            if (fBean.getGszzl() != null) {
                gszzlStr = fBean.getGszzl().startsWith("-") ? fBean.getGszzl().substring(1) : "+" + fBean.getGszzl();
            }
            String fixFundName = fBean.getFundName();
            if (fixFundName.startsWith("华")) fixFundName = "CPU-H";
            else if (fixFundName.startsWith("国")) fixFundName = "CPU-G";
            else if (fixFundName.startsWith("南")) fixFundName = "CPU-N";
            else if (fixFundName.startsWith("富")) fixFundName = "CPU-F";
            else fixFundName = "CPU-O";
            temp[i] = new Object[]{fixFundName, /*fBean.getGsz(),*/ gszzlStr + "%", timeStr};
        }
        return temp;
    }

    protected void clear() {
        data.clear();
    }
}
